<?
/*appname:Clsandir
 *internal version: 0001
 *version : 1.0.0   [programming] [official version]
 *author : HeShengyu
 *mail :koolfer@163.com
 *Date : 2014.07.21Clscandir
*/
class Clsandir
{
    public $path;
	public $sorting_order;
	public $completeFiles ;
	public $completeDirs ;
	public $files;
	public $dirs ;
	public $filesDirs ;
	public $completeFilesDirs ;
    public function  __construct($path,$sorting_order=0){
		$this->scandir($path , $sorting_order);
		$this->get_complete_filesDirs();
		$this->get_complete_dirs();
		return $this->filesDirs;
	}
	public function  scandir($path,$sorting_order=0){
		$this->path = $path ;
		$this->sorting_order = $sorting_order;
		$this->filesDirs=scandir ( $path , $sorting_order ) ;  
		return $this->filesDirs;       
		//sort()升序  rsort()降序 ;
	}
	public function get_complete_filesDirs(){
		if(count($this->completeFilesDirs)==0){
			foreach($this->filesDirs as $k => $v ){
				$this->completeFilesDirs[] = $this->_format_name($this->path."/".$v);   	
			}
		}
		return $this->completeFilesDirs;
		
	} 
	public function get_complete_dirs(){

		if(count($this->completeDirs)==0){
			foreach($this->completeFilesDirs as $k=>$v){
				
				if(is_dir($v)){
					$this->completeDirs[]=$v;
				}else{
					$this->completeFiles[]=$v;	
				}	
			}
		}
		return $this->completeDirs;
		
	}
	public function get_complete_files(){
		return $this->completeFiles;
	}
	private function iva(){}
	protected function ote(){}
	
	public function _scandir($path){
	    $dir = dir($path);
	    $retarr = array( ) ;
        while (($file = $dir->read()) !== false)
        {  $retarr[] =  ($file) ;  }	
	    $dir->close();
	    $retarr["cwd"]=$this->_format_name(getcwd());
	    return $retarr;
    }
	
	public function _format_name($str){		
	    return preg_replace (     '/\/{1,}/' , '/' ,
	           str_replace ( '\\' , '/' , $str )   ) ;	
    }
	
	public function show(){
		echo "目录：<br>";
	    secho($this->completeDirs);	
		echo "文件：<br>";
		secho($this->completeFiles);	
	}
	public function demo(){
	    echo str_replace(array("\n"," "),array("<br>","&nbsp;"),
	htmlspecialchars('     $dir=_default_em($_REQUEST["dir"],\"I:/AppServ/www/_doll/");
     $scandir=new CLsandir($dir,0);
     secho($scandir->get_complete_dirs());
     secho($scandir->get_complete_files());') )	;
		
	}
	public function each($arr,$callback=""){
		if(!is_array($arr))return 'Is not array!';
		
		$retarr=array();
		foreach($arr as $k=>$v){
			if(""!=$callback)
			{$retarr[$k]=$callback($v);	}
			else
			{$retarr[$k]=$v;}		
		}
		return $retarr;
	}
	
		
	
}


?>